const app = getApp()
Page({
  data: {
    goodsList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    show:false,
    showQu:false,
    status:'',
    statuscolor:'',
    serverHost: app.config.serverHost,
    numbers:0,
    type:'primary',
    columns: ['热门', '在售', '下架'],
    columnsQu:['所有区域','金湾区','斗门区','香洲区']
  },
  //修改数量
  bianhua:function(event){
    console.log(event)
    this.setData({
      numbers:event.detail
    })
  },
  // 确认修改数量按钮
  onchanges: function (){
    var that=this;
    wx.showModal({
      title: '更改数量',
      content: '确认要更改数量吗？',
      async success(res) {
        if (res.confirm) {
          let temp = 'goodsList[0].number'
          that.setData({
            [temp]:that.data.numbers
          })
          var updatas={
            _id: that.data.goodsList[0]._id,
            number: that.data.numbers
          }
          await app.api.puts("admin/goodslist/number", updatas, (res) => {})  
          wx.showToast({
            title: '修改成功',
            icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
            duration: 1000,      //停留时间
          })

        }
      }
    })
  },
  //修改状态
  xiugaistatus:function(){
    this.setData({
      show:true
    })
  },
  // 关闭修改状态
  onClose(){
    this.setData({ show: false,showQu:false })
  },
  // 确认修改状态
  onConfirm(event) {
    var that = this;
    const { picker, value, index } = event.detail
    that.setData({
      status: value
    })
    var updatas={
      _id: that.data.goodsList[0]._id,
      status: that.data.status
    }
    app.api.puts("admin/goodslist/status",updatas,(res)=>{})
    if(this.data.status == "热门") {
      this.setData({
        type: 'info',
        statuscolor: '#c50020'
      })
    }else if(this.data.status == "在售") {
      this.setData({
        type: 'primary',
        statuscolor: '#006B60'
      })
    }else{
      this.setData({
        type: 'warning',
        statuscolor: '#393A3F'
      })
    }
    this.setData({ show: false })
  },
  // 修改区域
  xiugaiqu(){
    this.setData({showQu:true})
  },
  // 确认修改区域
  confirmQu(e){
    let temp = 'goodsList[0].area'
    let updates = {
      _id: this.data.goodsList[0]._id,
      area:e.detail.value
    }
    this.setData({
      showQu:false,
      [temp]:e.detail.value
    })
    app.api.puts("admin/goodslist/area",updates,(res)=>{})
  },
  onLoad(e){
    let appData = app.globalData.goodslist
    let tempData = appData.filter(item=>{
      return item._id == e.id
    })
    tempData[0].shelves_time = tempData[0].shelves_time.split('T')[0]
    this.setData({
      goodsList: tempData,
      numbers: parseInt(tempData[0].number),
      status:tempData[0].status
    })
    if(this.data.status == "热门"){
      this.setData({
        type: 'info',
        statuscolor:'#c50020'
      })
    }else if(this.data.status == "在售"){
      this.setData({
        type: 'primary',
        statuscolor: '#006B60'
      })
    }else{
      this.setData({
        type: 'warning',
         statuscolor: '#393A3F'
      })
    }
  }
})